
var draggedobj=null;
var activeframe=null;
var win_start_x;
var win_start_y;
var mouse_start_x;
var mouse_start_y;

function init () {
}

function is_delete_div ( obj ) {
	return obj
			&& obj.hasAttribute
			&& obj.hasAttribute('id')
			&& obj.id.substr(obj.id.length-7) == "_delete"
			&& obj.parentNode
			&& obj.parentNode.parentNode
			&& obj.parentNode.parentNode.id == obj.id.substr(0,obj.id.length-7);
}

function is_move_div ( obj ) {
	return obj
			&& obj.hasAttribute
			&& obj.hasAttribute('id')
			&& obj.id.substr(obj.id.length-5) == "_move"
			&& obj.parentNode
			&& obj.parentNode.parentNode
			&& obj.parentNode.parentNode.id == obj.id.substr(0,obj.id.length-5);
}

function start_drag ( event, obj ) {
	draggedobj = obj.parentNode.parentNode;
	mouse_start_x = event.clientX;
	mouse_start_y = event.clientY;
	win_start_x = parseInt(draggedobj.style.left);
	win_start_y = parseInt(draggedobj.style.top);
}


function drag ( event ) {
	if ( draggedobj ) {
		draggedobj.style.left = win_start_x + (event.clientX - mouse_start_x);
		draggedobj.style.top = win_start_y + (event.clientY - mouse_start_y);
	}	
}

function stop_drag ( ) {
	draggedobj = null;
}

function switch_drag (event, obj) {
	if ( draggedobj ) {
		stop_drag();
	} else {
		start_drag(event,obj);
	}
}

function show_frame ( frameobj, event ) {
	if ( frameobj.style.visibility != "visible" ) {
		var basewidth = frameobj.parentNode.offsetWidth;
		var baseheight = frameobj.parentNode.offsetHeight;
		
		frameobj.style.width = basewidth + (basewidth < 20? 40:20);
		frameobj.style.left = basewidth < 20? -20:-10;

		frameobj.style.height = baseheight + (baseheight < 20? 40:20);
		frameobj.style.top = baseheight < 20? -20:-10;

		frameobj.style.visibility = "visible";
	}
	activeframe = frameobj;
}

function hide_frame ( frameobj ) {
	frameobj.style.visibility = "hidden";
	activeframe = null;
}

function deleteobj ( obj ) {
	obj.parentNode.style.visibility = "hidden";
	obj.parentNode.parentNode.style.visibility = "hidden";
}

function debug ( str ) {
	if ( !str )
		document.getElementById('debug').innerHTML = "";
	else
		document.getElementById('debug').innerHTML += str + "<br/>\n";
}

function check_move ( event ) {
	if ( draggedobj )
		drag(event);
	else if ( event.target && event.target.hasAttribute &&  event.target.hasAttribute('id') ) {
/*if ( event.shiftKey == 1 )*/
/* IE-nel nezni kene h van-e id */
			var actualframe = document.getElementById(event.target.id+"_frame");
			var actual = event.target;
			if ( activeframe != actualframe 
				/* && actual != activeframe */
				&& !(actual.parentNode 
						&& actual.parentNode.parentNode
						&& activeframe
						&& actual.parentNode.parentNode.id+"_frame" == activeframe.id
					) 
			) {
				if ( activeframe ) hide_frame(activeframe);
				if ( actualframe ) show_frame(actualframe,event);
			}
	}
/* else if ( activeframe ) hide_frame(activeframe); */

	//if (event.target) { debug(0); debug(event.target.id + ": " + event.target.style.zIndex ); }
}

function check_click ( event ) {
	if ( is_move_div ( event.target ) ) {
		switch_drag ( event, event.target );
	} else if ( is_delete_div ( event.target ) ) {
		deleteobj ( event.target );
	}
}

function newDel ( obj ) {
	var del=document.createElement('div');
	del.setAttribute('id', obj.id+'_delete');
	del.setAttribute('class', 'delete_div');
	return del;
}

function newMove ( obj ) {
	var move=document.createElement('div');
	move.setAttribute('id', obj.id+'_move');
	move.setAttribute('class', 'move_div');
}

function showFrame ()

function newFrame ( obj ) {
	var frame=document.createElement('div');
	frame.setAttribute('id', obj.id + '_frame');
	frame.setAttribute('class', 'frame_div');
	
	frame.appendChild(newMove(obj));
	frame.appendChild(newDel(obj));
	
	obj.onmouseover = showFrame;
	
	return frame;
}

function addFrame ( obj ) {
	obj.appendChild(newFrame(obj));
}
